import { defineStore } from 'pinia'
import dayjs from 'dayjs';
import http from '@/static/js/request';
import util from '@/static/js/util';

interface ArticleSate {
    userInfo: Record<string, any>
    token: string | null
    temToken: string | null
}
export const useArticleStore = defineStore({
    id: 'userStore',
    state: ():ArticleSate => ({
        userInfo: {},
        token: uni.getStorageSync('accessToken') || null,
        temToken: null
    }),
    getters: {
        
    },
    actions: {
        handleLike(params: Object) {
            return new Promise((resolve, reject) => {
                http('post', '/admin/likes', params, (data,msg) => {
                    resolve(data);
                })
            });
        },
        deleteLike(id:String){
            return new Promise((resolve, reject) => {
                http('delete', '/admin/likes?articlesId=' + id, {}, (data,msg) => {
                    resolve(data);
                })
            });
        },
        handleFavorite(params:Object) {
            return new Promise((resolve, reject) => {
                http('post', '/admin/favorites', params, (data,msg) => {
                    resolve(data);
                })
            });
        },
        deleteFavorite(id:String){
            return new Promise((resolve, reject) => {
                http('delete', '/admin/favorites?articlesId=' + id, {}, (data,msg) => {
                    resolve(data);
                })
            });
        },
        sendComment(params:Object){
            return new Promise((resolve, reject) => {
                http('post', '/admin/comments', params, (data,msg) => {
                    resolve(data);
                })
            });
        },
        deleteComment(id:String){
            return new Promise((resolve, reject) => {
                http('delete', '/admin/comments', [id], (data,msg) => {
                    resolve(data);
                })
            });
        },
        getImageList(url){
            if(url){
                let list = url.split(',');
                let arr = list.map(img => {
                    return util.getFullImage(img);
                });
                return arr;
            }else{
                return [];
            }
        },
        getImgHeight(){
            const res = uni.getSystemInfoSync();
            let width = res.windowWidth - 45 - 42 - 16;
            return (width / 3) + 'px';
        },
        getActivitiesName(obj, type){
            let name = obj.activitiesName;
            let time = dayjs(obj.startTime).format('YYYY-MM-DD');
            let isExpire = dayjs(obj.startTime).isBefore(dayjs(), 'day');
            let title = '报名一起聊！';
            if (isExpire) {
                title = '已完结~'
            }
            if (type == 'name') {
                return title + time + ' ' + name;
            }
            if (type == 'isExpire') {
                return isExpire;
            }
        }

    }
})
